package cn.wolfcode.wms.util;

import cn.wolfcode.wms.domain.Employee;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.Set;

public class UserContext {

    public static final String USER_IN_SESSION = "USER_IN_SESSION";
    public static final String EXPRESSION_IN_SESSION = "EXPRESSION_IN_SESSION";

    public static HttpSession getSession() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) (RequestContextHolder.getRequestAttributes());
        return requestAttributes.getRequest().getSession();
    }

    public static void setCurrentUser(Employee emp) {
        if (emp == null) {
            // 注销
            getSession().invalidate();
        } else {
            getSession().setAttribute(USER_IN_SESSION, emp);
        }
    }

    public static void setExps(Set<String> exps) {
        getSession().setAttribute(EXPRESSION_IN_SESSION, exps);
    }

    public static Employee getCurrentUser() {
        return (Employee) getSession().getAttribute(USER_IN_SESSION);
    }

    public static Set<String> getExps() {
        return (Set<String>) getSession().getAttribute(EXPRESSION_IN_SESSION);
    }
}
